<template>
	<view class="searchLayout">
		<custom-navbar titleText="搜索页" @getNavBarHeight="handleNavBarHeight"></custom-navbar>
		<view class="search" :style="'margin-top:'+navbarHeight+'rpx;'">
			<uni-search-bar @confirm="onSearch" @cancel="onClear" @clear="onClear" focus placeholder="搜索"
				v-model="queryParams.keyword">
			</uni-search-bar>
		</view>

		<view v-if="!classList.length || noSearch">
			<view class="history">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="removeHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>

			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in recommendList" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>
		</view>

		<view class="noSearch" v-if="noSearch">
			<uv-empty mode="search"></uv-empty>
		</view>

		<!-- 医院列表 -->
		<view v-else class="weui-cells hosp-list">
			<view v-for="(item, index) in hospitals" :key="item._id" :data-hid="item._id" @click="toHospital"
				class="weui-cell hosp-item weui-cell_access">
				<view class="weui-cell__hd">
					<image class="hosp-avatar" mode="aspectFill"
						:src="item.avatar ? item.avatar_url : '/static/images/avatar.jpg'">
					</image>
				</view>
				<view class="weui-cell__bd">
					<view>
						<text class="hosp-name">{{ item.name }}</text>
					</view>
					<view class="hosp-line">
						<text class="hosp-rank">{{ item.rank }}</text>
						<text class="hosp-label">{{ item.label }}</text>
					</view>
					<view class="hosp-line">
						<text class="hosp-intro">{{ item.intro }}</text>
					</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue';
	import {
		onLoad,
		onUnload,
		onReachBottom
	} from "@dcloudio/uni-app";
	// navbar高度
	const navbarHeight = ref(0);
	// 获取custom-navbar组件高度
	const handleNavBarHeight = (height) => {
		navbarHeight.value = height;
	}
	//查询参数
	const queryParams = ref({
		pageNum: 1,
		pageSize: 12,
		keyword: ""
	});
	//搜索历史词
	const historySearch = ref(uni.getStorageSync("historySearch") || []);
	//热门搜索词
	const recommendList = ref(["美女", "帅哥", "宠物", "卡通"]);
	//搜索结果列表
	const classList = ref([]);
	//没有更多
	const noData = ref(false);
	//没有搜索结果
	const noSearch = computed(() => {
		// 每次 hospitals 变化时，这里会重新执行
		return hospitals.value.length === 0;
	});

	//点击搜索
	const onSearch = () => {
		if (queryParams.value.keyword == '') {
			return;
		}
		uni.showLoading()
		historySearch.value = [...new Set([queryParams.value.keyword, ...historySearch.value])].slice(0, 10);
		initParams(queryParams.value.keyword);
		searchData();
		uni.setStorageSync("historySearch", historySearch.value);
		uni.hideLoading()
	}

	//点击清空搜索记录
	const removeHistory = () => {
		uni.showModal({
			title: "是否清空历史搜索？",
			success: res => {
				if (res.confirm) {
					uni.removeStorageSync("historySearch");
					historySearch.value = []
				}
			}
		})
	}

	//点击标签进行搜索
	const clickTab = (value) => {
		console.log('value', value);
		initParams(value);
		onSearch();
	}

	const initParams = (value = '') => {
		classList.value = [];
		noData.value = false;
		noSearch.value = false;
		queryParams.value = {
			pageNum: 1,
			pageSize: 12,
			keyword: value || ""
		}
	}
	// 定义医院详情的数据
	const hospitals = ref([]);
	// 云对象
	const accompanyHospitalObj = uniCloud.importObject("accompanyHospitalObj");
	// 云对象方法
	const searchData = async () => {
		let res = await accompanyHospitalObj.search(queryParams.value.keyword);
		hospitals.value = res;
		console.log('hospitals.value', hospitals.value);
	}

	//关闭有页面
	onUnload(() => {
		uni.removeStorageSync("storgClassList", classList.value);
	})
</script>

<style lang="scss" scoped>
	.searchLayout {
		width: 100vw;
		/* 宽度占满屏幕 */
		min-height: 100vh;
		/* 高度至少占满屏幕（避免内容少的时候背景不完整） */
		background-color: #fff;

		.topTitle {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 32rpx;
			color: #999;
		}

		.history {
			padding: 30rpx;
		}

		.recommend {
			padding: 30rpx;
		}

		.tabs {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			padding-top: 20rpx;

			.tab {
				background: #F4F4F4;
				font-size: 28rpx;
				color: #333;
				padding: 10rpx 28rpx;
				border-radius: 50rpx;
				margin-right: 20rpx;
				margin-top: 20rpx;
			}
		}

		.noSearch {
			margin-top: 100rpx;
		}
	}

	.hosp-avatar {
		display: block;
		width: 200rpx;
		height: 180rpx;
		border-radius: 10rpx;
		overflow: hidden;
		margin-right: 20rpx;
	}

	.hosp-name {
		font-weight: bold;
		font-size: 34rpx;
	}

	.hosp-line {
		margin-top: 5rpx;
	}

	.hosp-line text {
		font-size: 26rpx;
	}

	.hosp-rank {
		font-weight: bold;
		color: #0bb585;
		margin-right: 15rpx;
	}

	.hosp-label {
		font-weight: bold;
		color: #0ca7ae;
		margin-right: 15rpx;
	}

	.hosp-intro {
		color: #999999;
	}
</style>